package com.team24.hotel.service.impl;

import com.team24.hotel.domain.Permission;
import com.team24.hotel.mapper.PermissionMapper;
import com.team24.hotel.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<Permission> findAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public List<Permission> findByRoleId(Integer roleId) {
        return permissionMapper.selectAllByRoleId(roleId);
    }

    @Override
    public Permission findByPrimaryKey(Integer permissionId) {
        return permissionMapper.selectByPrimaryKey(permissionId);
    }

    @Override
    public int saveOrUpdate(Permission permission) {
        if (permission.getPermissionId()!=null){
            return permissionMapper.updateByPrimaryKey(permission);
        }else {
            return permissionMapper.insert(permission);
        }
    }

    @Override
    public int deletePermission(Integer permissionId) {
        permissionMapper.deletePermissionAndRole(permissionId);
        return permissionMapper.deleteByPrimaryKey(permissionId);
    }

    @Override
    public List<Permission> findAllByUserId(String userId) {
        return permissionMapper.selectAllByUserId(userId);
    }

    @Override
    public List<Permission> findAllByUserIdAndParentId(String userId, Integer parentPermissionId) {
        return permissionMapper.selectAllByUserIdAndParentId(userId,parentPermissionId);
    }

    @Override
    public List<Permission> findAllNoParentByUserId(String userId) {
        return permissionMapper.selectAllNoParentByUserId(userId);
    }
}
